﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using WebApplication1.API返回类;
using WebApplication1.Models.商品信息.地址;
using WebApplication1.Models.商品信息.屠宰;

namespace WebApplication1.Controllers
{
    /// <summary>
    /// 地区控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AddressController : ControllerBase
    {
        private readonly ISqlSugarClient _db;
        private readonly IMapper mapper;

        public AddressController(SqlSugar.ISqlSugarClient db,IMapper mapper)
        {
            this._db = db;
            this.mapper = mapper;
        }
        /// <summary>
        /// 省会查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<List<ProvincialModel>> GetProvincialAll()
        {
            var list = _db.Queryable<ProvincialModel>();

            return new APIResult<List<ProvincialModel>>()
            {
                Code = APIEnum.成功,
                Message = "查询省会成功",
                Data = list.ToList(),
            };
        }

        /// <summary>
        /// 市区查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<List<CityModel>> GetCityAll(int? ProvincialId)
        {
            var list = _db.Queryable<CityModel>();
            if(ProvincialId!=null)
            {
                list = list.Where(x => x.ProvincialId == ProvincialId);
            }
            return new APIResult<List<CityModel>>()
            {
                Code = APIEnum.成功,
                Message = "查询市区成功",
                Data = list.ToList(),
            };
        }

        /// <summary>
        /// 县查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<List<CommpanyAddressModel>> GetAddressAll(int? ProvincialId,int? CityId)
        {
            var list = _db.Queryable<CommpanyAddressModel>();
            if (ProvincialId != null)
            {
                list = list.Where(x => x.ProvincialId == ProvincialId);
            }
            if (CityId != null)
            {
                list = list.Where(x => x.CityId == CityId);
            }
            return new APIResult<List<CommpanyAddressModel>>()
            {
                Code = APIEnum.成功,
                Message = "查询县成功",
                Data = list.ToList(),
            };
        }
    }
}
